package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.BaiAnswer;
import com.ruoyi.system.service.IBaiAnswerService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 健康咨询Controller
 * 
 * @author saruri
 * @date 2024-08-18
 */
@RestController
@RequestMapping("/answer")
public class BaiAnswerController extends BaseController
{
    @Autowired
    private IBaiAnswerService baiAnswerService;

    /**
     * 查询健康咨询列表
     */

    @GetMapping("/list")
    public TableDataInfo list(BaiAnswer baiAnswer)
    {
        startPage();
        List<BaiAnswer> list = baiAnswerService.selectBaiAnswerList(baiAnswer);
        return getDataTable(list);
    }

    /**
     * 导出健康咨询列表
     */
    @RequiresPermissions("system:answer:export")
    @Log(title = "健康咨询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaiAnswer baiAnswer)
    {
        List<BaiAnswer> list = baiAnswerService.selectBaiAnswerList(baiAnswer);
        ExcelUtil<BaiAnswer> util = new ExcelUtil<BaiAnswer>(BaiAnswer.class);
        util.exportExcel(response, list, "健康咨询数据");
    }

    /**
     * 获取健康咨询详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baiAnswerService.selectBaiAnswerById(id));
    }

    /**
     * 新增健康咨询
     */
    @RequiresPermissions("system:answer:add")
    @Log(title = "健康咨询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaiAnswer baiAnswer)
    {
        baiAnswer.setAnswerUserId(SecurityUtils.getUserId());
        System.out.println(baiAnswer);
        return toAjax(baiAnswerService.insertBaiAnswer(baiAnswer));
    }

    /**
     * 修改健康咨询
     */
    @RequiresPermissions("system:answer:edit")
    @Log(title = "健康咨询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaiAnswer baiAnswer)
    {
        return toAjax(baiAnswerService.updateBaiAnswer(baiAnswer));
    }

    /**
     * 选中健康咨询答案
     */
    @RequiresPermissions("system:answer:edit")
    @Log(title = "选中健康咨询答案", businessType = BusinessType.UPDATE)
    @PutMapping("chose")
    public AjaxResult chose(@RequestBody BaiAnswer baiAnswer)
    {
        System.out.println("选中了答案 动作如下");
        System.out.println(BusinessType.UPDATE);
        System.out.println( baiAnswer);
        //增加一个 让该问题的其他答案status为0 如何写
       // return toAjax(baiAnswerService.updateBaiAnswer(baiAnswer));
        return toAjax(baiAnswerService.chose(baiAnswer));
    }

    /**
     * 删除健康咨询
     */
    @RequiresPermissions("system:answer:remove")
    @Log(title = "健康咨询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baiAnswerService.deleteBaiAnswerByIds(ids));
    }
}
